package com.group4.Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.group4.DAO.PostDAO;
import com.group4.DB.Post;


@WebServlet( name="ListTopic", displayName="ListTopic", urlPatterns = {"/topic"})
public class ListTopic extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2612550550906995557L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		// Get the printwriter object from response to write the required json
		// object to the output stream
		
		
		String topicId = request.getParameter("topicId");
		PrintWriter out = response.getWriter();
		List<PostDAO> postList = Post.getPostList(Integer.valueOf(topicId));
		Gson gson = new GsonBuilder().setDateFormat("yyyy/MM/dd HH:mm:ss").create();
		String json = gson.toJson(postList);
		out.print(json);
		out.flush();
		
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		// Get the printwriter object from response to write the required json
		// object to the output stream
		
		

		String topicId = request.getParameter("topicId");
		PrintWriter out = response.getWriter();
		List<PostDAO> postList = Post.getPostList(Integer.valueOf(topicId));
		Gson gson = new GsonBuilder().setDateFormat("yyyy/MM/dd HH:mm:ss").create();
		String json = gson.toJson(postList);
		out.print(json);
		out.flush();
		
		
	}
}
